package com.ophiux.websocket.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @desc:
 * @author: hhl
 * @date：2023/10/12 10:13
 */
public class Test {

    public static void main(String[] args) {

        CountDownLatch latch = new CountDownLatch(4);
        for (int i = 0; i < 5; i++) {

            CompletableFuture.runAsync(() -> {
                try {
                    System.out.println("当前线程："+Thread.currentThread().getName()+"开始执行");
                    TimeUnit.SECONDS.sleep(2);
                    //计数器减1
                    latch.countDown();
                    System.out.println("当前线程："+Thread.currentThread().getName()+"执行完成,计数器="+latch.getCount());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

        try {
            latch.await();
            System.out.println(Thread.currentThread().getName()+",计数器="+latch.getCount());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
